package com.ldcc.report_system.component;

import android.app.DatePickerDialog;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.ldcc.report_system.R;

import java.util.Calendar;
import java.util.Locale;

/**
 * @author wwj[yeshengkele@outlook.com]
 * @description 时间选择组件
 */
public class DataSelectionLayout extends LinearLayout {

    private TextView dataPickerYear;
    private TextView dataPickerMonth;
    private TextView dataPickerDay;
    private int year;
    private int month;
    private int day;

    public DataSelectionLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.date_selection_layout, this);
        this.dataPickerYear = findViewById(R.id.date_picker_year);
        this.dataPickerMonth = findViewById(R.id.date_picker_month);
        this.dataPickerDay = findViewById(R.id.date_picker_day);
        // 设置初始化日期
        //  获取当前时间  —   年、月、日
        Calendar calender = Calendar.getInstance();
        this.setDate(calender.get(Calendar.YEAR), calender.get(Calendar.MONTH) + 1, calender.get(Calendar.DAY_OF_MONTH));
        // 设置默认点击事件
        setOnClickListener(view -> {
            this.year = Integer.parseInt(this.dataPickerYear.getText().toString());
            this.month = Integer.parseInt(this.dataPickerMonth.getText().toString()) - 1;
            this.day = Integer.parseInt(this.dataPickerDay.getText().toString());
            //  日期选择对话框
            DatePickerDialog datePickerDialog = new DatePickerDialog(context, R.style.AppDatePickerDialogTheme);
            datePickerDialog.updateDate(this.year, this.month, this.day);
            datePickerDialog.show();
            datePickerDialog.setOnDateSetListener((dataPickerView, year, month, dayOfMonth) -> {
                setDate(year, month + 1, dayOfMonth);
            });
        });
    }

    /**
     * @param year       年
     * @param month      月
     * @param dayOfMonth 日
     * @description 设置界面上的年月日
     */
    public void setDate(int year, int month, int dayOfMonth) {
        this.year = year;
        this.month = month;
        this.day = dayOfMonth;
        // 调用setText()方法时，传入的int值，会被当成resourceID来使用，
        this.dataPickerYear.setText(String.valueOf(this.year));
        this.dataPickerMonth.setText(String.format(Locale.CHINA, "%02d", this.month));
        this.dataPickerDay.setText(String.format(Locale.CHINA, "%02d", this.day));
    }


    public TextView getDataPickerYear() {
        return dataPickerYear;
    }

    public void setDataPickerYear(TextView dataPickerYear) {
        this.dataPickerYear = dataPickerYear;
    }

    public TextView getDataPickerMonth() {
        return dataPickerMonth;
    }

    public void setDataPickerMonth(TextView dataPickerMonth) {
        this.dataPickerMonth = dataPickerMonth;
    }

    public TextView getDataPickerDay() {
        return dataPickerDay;
    }

    public void setDataPickerDay(TextView dataPickerDay) {
        this.dataPickerDay = dataPickerDay;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }
}
